package puzzle.projecteuler.p200;

import java.util.HashMap;
import java.util.Map;

import astudy.util.AdvMath;
import astudy.util.AdvMath.Factors;

public class Problem127B {

	public static int[] rads = null;
 
	/**
	 * 注意:a,b,c 两两互素，
	 * 所以rad(abc) = rad(a) * rad(b) * rad(c)
	 * 
	 * answer	: 150401
	 * time cost: 3203 ms
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		long s = System.currentTimeMillis();
		
		int m = 1000;
		init(m);
		System.out.println("init: " + (System.currentTimeMillis() - s) + " ms");
		int count = 0;
		long sum = 0;
		for (int c = 1; c < m; c ++) {
			int t = getCount(c);
			count += t;
			sum += c*t;
		}
		System.out.println("count \t: " + count);
		System.out.println("answer\t: " + sum);
		System.out.println("time cost: " + (System.currentTimeMillis() - s) + " ms");
	}
	
	/**
	 * 初始化factors
	 * @param n
	 */
	public static void init(int m) {
		
		Integer[] ps = AdvMath.primes(m);
		Map<Integer, Factors> factors = new HashMap<Integer, Factors>();
		for (int i = 1; i <= m; i ++) {
			factors.put(i, new Factors());
		}
		for (int i = 0; i < ps.length; i ++) {
			int p = ps[i];
			for (int j = p; j < m; j += p) {
				factors.get(j).put((long)p, 1);
			}
		}
		
		rads = new int[m];
		for (int i = 0; i < rads.length; i ++) {
			Long t = 1L;
			if (factors.get(i) != null) {
				for (long p: factors.get(i).keySet()) { t *= p; }
				rads[i] = t.intValue();
			}
		}
	}
	
	/**
	 * @param c
	 * @return
	 */
	public static int getCount(int c) {

		int count = 0;
		for (int a = 1; a < c/2; a ++) {
//			if (AdvMath.gcd(a, c) == 1) {
//				if (rads[a] * rads[c-a] * rads[c] < c) {
//					count ++;
//				}
//			}
		}
		return count;
	}
}
